package com.seeyon.apps.ext.cop.button.util;

/**
 * Created by zouhao on 16/1/11.
 *
 * @ClassName: ${ClassName}
 * @Description: TODO
 * @author: zouhao
 * @date: 16/1/11
 */


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;

public class ResterfulUtil {
	private final static Log LOG = LogFactory.getLog(ResterfulUtil.class);
    public String load(String url2,String query) throws Exception
    {
    	LOG.info(query);
    	String cleanedString = query.replaceAll("null","");
    	 URL url = new URL(url2);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept", "application/json");
        httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
        httpURLConnection.setRequestProperty("contentType", "utf-8");
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        
        OutputStream outputStream = httpURLConnection.getOutputStream();
        outputStream.write(cleanedString.getBytes("UTF-8"));
        outputStream.flush();

       // BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
        InputStreamReader in = null;
        in = new InputStreamReader(httpURLConnection.getInputStream(),"utf-8");
        BufferedReader bufferedReader = new BufferedReader(in);
        String output="";
        StringBuffer buffer = new StringBuffer();
        while ((output = bufferedReader.readLine()) != null ) {
           // System.out.print(output);
        	buffer.append(output);
        }
        String str = buffer.toString();
        LOG.info(str);
		return str;
    
    }
    

    public static void main(String []args) {

        try {
        	String token=md5("11111f185f3a7-b6f9-402d-bc7e-2aa3acb74c3d2020-04-15 12:01:27");
			String orgpost="{\"appid\":\"11111\",\"time\":\"2020-04-15 12:01:27\",\"token\":\""+token+"\",\"data\":[{\"def10\":\"e4f18d94-40cd-4cdf-84d6-0b0ad9540d8d\",\"org_account_id\":\"\",\"ext_attr_1\":\"aaa\",\"ext_attr_2\":\"bbb\",\"code\":\"test001\",\"dr\":1,\"enablestate\":2,\"name\":\"测试组2\"}]}";
			
			ResterfulUtil resterfulUtil = new ResterfulUtil();

            String resultString = resterfulUtil.load("http://219.132.199.194:82/service/~jzpm/nc.http.jzpm.jzbpm.receivedata.ReceiveJzpmBpmFlowServlet",orgpost.toString());
            System.out.print(resultString);
            
            } catch (Exception e) {

            // TODO: handle exception

            System.out.print(e.getMessage());

            }

        }
    
    
    public static String md5(String s)
    {
        char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    }
